<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- <div ></div> -->
    <table id="app" border="1" cellspacing="0">
        <thead>
            <tr>
                <th>全选<input type="checkbox" v-model="allCheck" @change="all"></th>
                <th>名称</th>
                <th>单价</th>
                <th>数量</th>
                <th>小计</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="(item, index) in list">
                <td><input type="checkbox" v-model="item.isCheck" @change="item1"></td>
                <td>{{list[index].name}}</td>
                <td>{{list[index].price}}</td>
                <td><input type="number" v-model="list[index].num"></td>
                <td>{{list[index].num * list[index].price}}</td>
            </tr>
            <tr>
                <td>-</td>
                <td colspan="2">total</td>
                <td>{{totalNum}}</td>
                <td>{{totalPrice}}</td>
            </tr>
        </tbody>
    </table>
    <script src="../vue.js"></script>
    <script>
        let vm = new Vue({
            el: '#app',
            data: {
                // 是否全选
                allCheck: false,
                list: [
                    {
                        "name": "【DCH-30170200058】东成_合金钻头6*160圆柄(60202024)/[1支]",
                        "price": 3,
                        num: 5,
                        // 当前商品是否选中
                        isCheck: false
                    },
                    {
                        "name": "博世（BOSCH）S3 四坑两刃钻头 12*400*460mm-(2608680280)/支",
                        "price": 26,
                        num: 4,
                        isCheck: false
                    },
                    {
                        "name": "【DCH-30170200038】东成_合金钻头12*150圆柄(60202004)/[1支]",
                        "price": 4,
                        num: 7,
                        isCheck: false
                    },
                    {
                        "name": "【DCH-30170200037】东成_合金钻头10*150圆柄(60202003)/[1支]",
                        "price": 4,
                        num: 5,
                        isCheck: false
                    },
                ]
            },
            computed: {
                totalNum() {
                    let num = 0;
                    for (let i = 0; i < this.list.length; i++) {
                        if (this.list[i].isCheck) {
                            // 勾选时才会计算
                            num += this.list[i].num * 1 // 转为数字
                        }
                    }
                    return num + '件'
                },
                totalPrice() {
                    let num = 0;
                    for (let i = 0; i < this.list.length; i++) {
                        if (this.list[i].isCheck) {
                            // 勾选时才会计算
                            num += this.list[i].num * this.list[i].price
                        }
                    }
                    return '¥' + num + '元'
                }
            },
            methods: {
                // 全选
                all() {
                    this.list.forEach(item => {
                        item.isCheck = this.allCheck
                    });
                },
                // 选择单个商品
                item1() {
                    // every 遍历数组中的每一项。如果每一项都符合return后的条件，就返回true；否则是false
                    this.allCheck = this.list.every(item => {
                        return item.isCheck
                    })
                }
            }
        })
    </script>
</body>

</html>